<?php namespace Admin\Controller;

use Hdphp\Controller\Controller;

//登录控制器
class LoginController extends Controller{
	private $model;
	public function __init(){
		$this->model=new \Admin\Model\Admin;
	}

	//登录
	public function index(){
		//登录验证
		if (IS_POST) {
			//判断验证码是否正确
			if (!$this->model->checkCode()) View::error('验证码不正确');
			//验证用户名是否正确
			$username=Q('post.username');
			$data=$this->model->where("adminaccount='{$username}'")->find();
			if (!$data) View::error('用户名或密码不正确');
			//验证密码是否正确
			$password=Q('post.password','','md5');
			if ($data['adminpwd'] != $password) View::error('用户名或密码不正确');
			//登录成功
			$_SESSION['aid']=$data['aid'];
			$_SESSION['adminaccount']=$data['adminaccount'];
			$_SESSION['adminname']=$data['adminname'];
			//更新登录时间和ip地址
			$this->model->where("aid={$data['aid']}")->update(['logintime'=>time(),'loginip'=>$_SERVER['REMOTE_ADDR']]);
			$this->model->where("aid={$data['aid']}")->increment('times',1);
			View::success('登录成功',U('Index/index'));
		}
		//显示模板
		View::make();
	}

	//验证码
	public function code(){
		Code::num(1)->height(43)->make();
	}

	//退出登录
    public function quit(){
    	session_unset();
    	session_destroy();
    	go(U('index'));
    }

}